package com.paw.ruoyi.admin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.paw.ruoyi.admin.common.constant.CommonConstant;
import com.paw.ruoyi.admin.biz.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author Rubble
 * @date 2022/1/12
 **/
@Slf4j
@Component
public class TokenInterceptor implements HandlerInterceptor {

  @Autowired
  private TokenService tokenService;

  @Override
  public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    log.info("TokenInterceptor uri: {}", request.getRequestURI());
    String token  = request.getHeader(CommonConstant.TOKEN);
    return tokenService.verifyToken(token);
  }

  @Override
  public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//    AdminUserContext.remove();
  }
}
